﻿
using Chain.BLL;
// using ChainStock.Controls;
using System;
using System.Data;
using System.Text;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using Wuqi.Webdiyer;
using System.Data.SqlClient;
using System.Runtime.InteropServices;

using Chain.DBUtility;
using Chain.Model;

public partial class RptPointRate : PageBase
{

    public static string MemCardId;

    private StringBuilder strwhere = new StringBuilder();
    private readonly Chain.IDAL.PointRate dal = new Chain.IDAL.PointRate();
    public DataSet GetListSP(int PageSize, int PageIndex, out int resCount, string strTime, params string[] strWhere)
    {
        return this.dal.GetListSP(PageSize, PageIndex, out resCount, strTime, strWhere);
    }

    private void BindPointChange()
    {
        Chain.BLL.PointRate bllPointRate = new Chain.BLL.PointRate();
        this.hidLevel.Value = bllPointRate.GetPointRate().PointRateLevel.ToString();
        int intPoint = 0;
        string strPoint = "";
        string sbWhere = this.QueryCondition().Replace("POINTLOG.PointChangeType =9", "(POINTLOG.PointChangeType =9 or POINTLOG.PointChangeType =12)") + "  and PointLog.PointShopID = SysShop.ShopID and PointLog.PointMemID = Mem.MemID and Mem.MemLevelID=MemLevel.LevelID and PointLog.PointUserID = SysUser.UserID";
        sbWhere = PubFunction.GetShopAuthority(base._UserShopID, "PointShopID", sbWhere);
        intPoint = bllPointRate.GetPointRateNumber(sbWhere + " and datediff(day,PointCreateTime,getdate())=0");
        strPoint = string.Format("{0}", intPoint);
        this.lblToday.Text = strPoint;
        intPoint = bllPointRate.GetPointRateNumber(sbWhere + " and datediff(week,PointCreateTime,getdate())=0");
        strPoint = string.Format("{0}", intPoint);
        this.lblWeek.Text = strPoint;
        intPoint = bllPointRate.GetPointRateNumber(sbWhere + " and datediff(month,PointCreateTime,getdate())=0");
        strPoint = string.Format("{0}", intPoint);
        this.lblMonth.Text = strPoint;
        intPoint = bllPointRate.GetPointRateNumber(sbWhere);
        strPoint = string.Format("{0}", intPoint);
        this.lblTotal.Text = strPoint;
    }

    protected void btnRptPointRateExcel_Click(object sender, EventArgs e)
    {
        string strStartTime = this.txtStartTime.Value;
        string strEntTime = this.txtEndTime.Value;
        StringBuilder strTime = new StringBuilder();
        string strSql = this.QueryCondition();
        if (strStartTime != "")
        {
            this.strwhere.AppendFormat(" and POINTCREATETIME>='{0}'", strStartTime);
            strTime.AppendFormat(" and PointLOG.POINTCREATETIME>='{0}' ", strStartTime);
        }
        if (strEntTime != "")
        {
            this.strwhere.AppendFormat(" and POINTCREATETIME<='{0}'", strEntTime);
            strTime.AppendFormat(" and PointLOG.POINTCREATETIME<='{0}'", strEntTime);
        }
        Chain.BLL.PointRate bllPointRate = new Chain.BLL.PointRate();
        int Counts = this.NetPagerParameter.RecordCount;
        strSql = (strSql + "and MEM.MEMID<>0 AND MEM.MEMSHOPID=SYSSHOP.SHOPID") + " AND MEM.MEMLEVELID=MEMLEVEL.LEVELID" + " AND MEM.MEMUSERID=SYSUSER.USERID";
        if (MemCardId != null)
        {
            strSql = strSql + string.Format(" and Mem.MemCard={0}", MemCardId);
        }
        strSql = PubFunction.GetShopAuthority(base._UserShopID, "MemShopID", strSql);
        DataTable db = GetListSP(Counts, this.NetPagerParameter.CurrentPageIndex, out Counts, strTime.ToString(), new string[] { strSql }).Tables[0];
        DataExcelInfo.PointRateExcel(db, base._UserName, this.strwhere.ToString());
    }

    protected void btnRptPointRateQuery_Click(object sender, EventArgs e)
    {
        this.strwhere = new StringBuilder();
        this.BindPointChange();
        this.Get_ParameterList(this.QueryCondition());
    }

    protected void drpPageSize_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.NetPagerParameter.CurrentPageIndex = 1;
        this.NetPagerParameter.PageSize = int.Parse(this.drpPageSize.SelectedValue);
        this.Get_ParameterList(this.QueryCondition());
    }

    private void Get_ParameterList(string strSql)
    {
        string strStartTime = this.txtStartTime.Value;
        string strEntTime = this.txtEndTime.Value + " 23:59:59";
        StringBuilder strTime = new StringBuilder();
        if (strStartTime != "")
        {
            this.strwhere.AppendFormat(" and POINTCREATETIME>='{0}'", strStartTime);
            strTime.AppendFormat(" and PointLOG.POINTCREATETIME>='{0}' ", strStartTime);
        }
        if (strEntTime != "")
        {
            this.strwhere.AppendFormat(" and POINTCREATETIME<='{0}'", strEntTime);
            strTime.AppendFormat(" and PointLOG.POINTCREATETIME<='{0}'", strEntTime);
        }
        Chain.BLL.PointRate bllPointRate = new Chain.BLL.PointRate();
        int Counts = this.NetPagerParameter.RecordCount;
        strSql = strSql + "and MEM.MEMID<>0 AND MEM.MEMSHOPID=SYSSHOP.SHOPID";
        strSql = strSql + " AND MEM.MEMLEVELID=MEMLEVEL.LEVELID";
        strSql = strSql + " AND MEM.MEMUSERID=SYSUSER.USERID";
        if (MemCardId != null)
        {
            strSql = strSql + string.Format(" and Mem.MemCard={0}", MemCardId);
        }
        strSql = PubFunction.GetShopAuthority(base._UserShopID, "MemShopID", strSql);
        DataTable db = GetListSP(this.NetPagerParameter.PageSize, this.NetPagerParameter.CurrentPageIndex, out Counts, strTime.ToString(), new string[] { strSql }).Tables[0];
        this.NetPagerParameter.RecordCount = Counts;
        this.NetPagerParameter.CustomInfoHTML = string.Format("<div class=\"results\"><span>当前第{0}/{1}页 共{2}条记录 每页{3}条</span></div>", new object[] { this.NetPagerParameter.CurrentPageIndex, this.NetPagerParameter.PageCount, this.NetPagerParameter.RecordCount, this.NetPagerParameter.PageSize });
        this.rptRptPointRate.DataSource = db;
        this.rptRptPointRate.DataBind();
        PageBase.BindSerialRepeater(this.rptRptPointRate, this.NetPagerParameter.PageSize * (this.NetPagerParameter.CurrentPageIndex - 1));
    }

    protected void NetPagerParameter_PageChanging(object src, PageChangingEventArgs e)
    {
        this.NetPagerParameter.CurrentPageIndex = e.NewPageIndex;
        this.Get_ParameterList(this.QueryCondition());
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!base.IsPostBack)
        {
            int year = DateTime.Today.Year;
            int month = DateTime.Today.Month;
            int day = DateTime.Today.Day;
            this.txtStartTime.Value = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).ToString("yyyy-MM-dd");
            this.txtEndTime.Value = DateTime.Now.ToString("yyyy-MM-dd");
            PubFunction.BindMemLevelSelect(this.sltMemLevelID, true);

            this.BindPointChange();
            this.Get_ParameterList(this.QueryCondition());
        }
    }

    protected string QueryCondition()
    {
        string strQueryMem = this.txtQueryMem.Value;
        string strMemLevelID = this.sltMemLevelID.Value;

        StringBuilder strSql = new StringBuilder();
        strSql.Append(" POINTLOG.PointChangeType =9 ");
        if (strQueryMem != "")
        {
            strSql.AppendFormat("and (MEM.MemCard like '%{0}%' or MEM.MemName like '%{0}%' or MEM.MemMobile like '%{0}%' or MEM.MemCardNumber like '%{0}%' )", strQueryMem);
        }
        if (strMemLevelID != "")
        {
            strSql.AppendFormat("and MEM.MemLevelID={0}", int.Parse(strMemLevelID));
        }

        return strSql.ToString();
    }
}


